#include "game.h"

extern int shape[7][4][18];

void print_mode_shape(int n,int m,int x,int y,int c)//打印方块
{
    int i = 0;
    int xx = x;

    int yy = y;
    for(i = 0;i < 16;i++)
    {
        if(i != 0 && i%4 == 0)//防越界，换行
        {
            yy += 1;
            xx = x;
        }
        if(shape[n][m][i] == 1){
            printf("\033[%d;%dH\033[%dm[]\033[0m",yy,xx,c);
        }
        xx += 2;
    }
    fflush(NULL);//刷新缓冲区
}

void clear_block(int type,int change_type,int x,int y){//清除方块
    int xx =x;
    int yy=y;
    for (int i = 0; i < 16; i++)
    {
        //每画完四个要换行，x重置到最开始。y+1
        if (i!=0 && i%4==0)
        {
            yy = yy+1;
            xx =x;
        }
        if (shape[type][change_type][i]==1)
        {
            printf("\033[%d;%dH",yy,xx);
            printf("  ");
            printf("\033[0m");
        }
        //每画一个，xx就要移动2个字符
        xx+=2;
    }
    fflush(NULL);
}

void print_next_shape(int color){//打印下一个图形
        //清除原来
    if (next_type!=-1)
    {
       clear_block(next_type,next_change_type,next_x,next_y);
    }
    //打新的
    next_type = random() %7;
    next_change_type = random() %4;
    print_mode_shape(next_type,next_change_type,next_x,next_y,color);
}

void move_right(int type, int chageType, int color){//向右移
    if(check_collide(type, chageType, dynamic_x + 2, dynamic_y))//碰撞检查
        return;
    if(dynamic_x + 2 * (4 - shape[type][chageType][16]) - 1 > 38)//越界检查
       return;

    clear_block(type,chageType,dynamic_x,dynamic_y);
    print_mode_shape(type,chageType,dynamic_x += 2,dynamic_y,color);
}

void move_left(int type, int chageType, int color){//向左移
    if(dynamic_x < 13)//越界检查
      return;
    if(check_collide(type, chageType, dynamic_x - 2, dynamic_y))//碰撞检查
        return;
    clear_block(type,chageType,dynamic_x,dynamic_y);
    print_mode_shape(type,chageType,dynamic_x -= 2,dynamic_y,color);
}
int move_down(int type, int chageType, int color){//向下移
    int n = dynamic_y + 1 * (4 - shape[type][chageType][17]) - 1;
    //("out-->(dynamic_x:%d,dynamic_y:%d)\n", dynamic_x, dynamic_y + 1);
    bool collide = check_collide(type, chageType, dynamic_x, dynamic_y + 1);
    //越界检查和碰撞检查
    if( n >= 29 || collide){
       //把不能动的图存到二维数组中
       //printf("in-->(dynamic_x:%d,dynamic_y:%d)\n", dynamic_x, dynamic_y);
       store_cur_shape(type, chageType, color);
       //初始化新图
       init_new_shape(color);
       //消除行
       clear_full_row();
       //打印下一个图
       print_next_shape(color);
       return 1;
    }
    clear_block(type,chageType,dynamic_x,dynamic_y);
    print_mode_shape(type,chageType,dynamic_x,++dynamic_y,color);
    return 0;
}

void clear_full_row(){
    //遍历所有行
    int flag = 0;
    for (int i = 0; i < 24; i++)
    {
        //需要得到一个结果，看这行是否满了
        flag =1; //认为这行已经满了
        for (int j = 0; j < 28; j++)
        {
             // 只要有一个没有满就没满，跳出循环
             if (matrix[i][j]==0)
             {
                 flag =0;
                 break;
             }    
        }
        if (flag == 1)
        {
            for (int k = i; k > 0; k--)
            {
                for (int m = 0; m< 28; m++)
                {
                    matrix[k][m]=  matrix[k-1][m];
                }
            }
            for (int m = 0; m< 28; m++)
            {
                matrix[0][m]=0;
            }  
            
            //加分+动态计算等级
            score+=10;
            if (score!=0&& score%500==0)
            {
                level+=1;
                interval/=2;
                //重新设置下落速度
                alarm_us(interval);
            }
            //重新输出图形
            print_matrix();
            //并且打出来
            print_score_level();
      }  
    }
}
void print_score_level(){
    printf("\33[%d;%dH分数:%d\33[0m",score_y,score_x,score);
     printf("\33[%d;%dH等级:%d\33[0m",level_y,level_x,level);
     fflush(NULL);
}
void print_matrix(){
    for(int i = 0; i < 24; i++){
        for(int j = 0; j < 28; j += 2){
            int x = j + 12;
            int y = i + 6;
            if(matrix[i][j] == 0){
                //清除
                //printf("\033[%d;%dH\033[%dm[  \033[0m",x,y,color);
                printf("\033[%d;%dH",y,x);
                printf("\033[%dm  ",color_background);
                printf("\033[0m");
            }
            else{
                
                //重绘
                //printf("\033[%d;%dH\033[%dm[]\033[0m",x,y,color);
                printf("\033[%d;%dH",y,x);
                printf("\033[%dm[]",color);
                printf("\033[0m");
            }
        }
    }
    fflush(NULL);
}
void chageType(int x, int y, int color){//按上键变换图形
    int changType = (dynamic_changeType + 1) % 4;//拿到下一次变换的类型
    /*对下次的变换类型进行碰撞检查和越界检查*/
    bool down = check_collide(dynamic_type, changType, dynamic_x, dynamic_y + 1);
    if(dynamic_y + 1 * (4 - shape[type][changType][17]) - 1 >= 29)
        return;
    bool left = check_collide(dynamic_type, changType, dynamic_x , dynamic_y);
    if(dynamic_x < 13)
        return;
    bool right = check_collide(dynamic_type, changType, dynamic_x , dynamic_y);
    if(dynamic_x + 2 * (4 - shape[type][changType][16]) - 1 > 38)
       return;
    if(down || left ||right)    
        return;
    clear_block(dynamic_type,dynamic_changeType,x,y);
    dynamic_changeType = changType;
    print_mode_shape(dynamic_type,dynamic_changeType,x,y,color);
}

void move_button(int dynamic_type,int dynamic_change_type,int color){//向下掉落
    while(1){
        bool ret = move_down(dynamic_type, dynamic_change_type, color);
        //printf("%d\n", ret);
        if(ret) return;
    }
}

void store_cur_shape(int type, int change_type, int color){//存储当前图形在底部
    int row = dynamic_y - 6;
    int col = dynamic_x - 12;
    for(int i = 0; i < 16; ++i){
        if(i && i % 4 == 0){
            ++row;
            col = dynamic_x - 12;
        }
        if(shape[type][change_type][i] == 1){
            matrix[row][col] = color;
            matrix[row][col + 1] = color;
            //printf("row:%d,col:%d,matrix[row][col]:%d\n", row, col,matrix[row][col]);
            //printf("row:%d,col:%d,matrix[row][col]:%d\n", row, col + 1,matrix[row][col + 1]);
        }
        col += 2;
    }
}

void init_new_shape(int color){//初始化新的图形
    int type = next_type,change_type = next_change_type;
    int x = init_x,y = init_y;

    dynamic_type = type;
    dynamic_changeType = change_type;
    dynamic_x = x;
    dynamic_y = y;
    print_mode_shape(type, change_type, x, y, color);
}
bool check_collide(int type, int change_type, int x, int y){//碰撞检查
    int row = y - 6;
    int col = x - 12;
    for(int i = 0; i < 16; ++i){
        if(i != 0 && i % 4 == 0){
            ++row;
            col = x - 12;
        }
        if(shape[type][change_type][i] == 1){
            if(matrix[row][col]){
                //printf("check_collide-->row:%d col:%d matrix[row][col]:%d\n",row,col, matrix[row][col]);
                return true;
            }   
        }
        col += 2;
    }
    return false;
}